package com.xuecheng.content.controller;


import com.xuecheng.base.exception.ValidationGroups;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.base.utils.JsonUtil;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.EditCourseDto;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.service.CourseBaseInfoService;
import com.xuecheng.base.model.dto.PageParams;
import com.xuecheng.base.model.dto.PageResult;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.util.SecurityUtil;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author Hes
 */
@Slf4j
@RestController
@Api(value = "课程信息编辑接口", tags = "课程信息编辑接口")
public class CourseBaseInfoController {

    @Autowired
    private CourseBaseInfoService courseBaseInfoService;

    /**
     * @param pageParams
     * @param queryCourseParams
     * @return
     */
    @ApiOperation("课程信息列表接口")
    @PostMapping("/course/list")
//    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")
    public PageResult<CourseBase> list(PageParams pageParams, @RequestBody(required = false) QueryCourseParamsDto queryCourseParams) {
//        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        System.out.println(principal);
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        PageResult<CourseBase> list = courseBaseInfoService.list(pageParams, queryCourseParams);
        return list;
    }

    /**
     * 增加课程接口
     *
     * @param addCourseDto
     * @return 在Controller方法中启动校验规则指定要使用的分组名：@Validated({ValidationGroups.Inster.class}
     */
    @ApiOperation("增加课程接口")
    @PostMapping("/course")
    public CourseBaseInfoDto createCourseBase(@RequestBody @Validated({ValidationGroups.Inster.class})
                                              AddCourseDto addCourseDto) {
        Long companyId = 1232141425L;
        CourseBaseInfoDto courseBase = courseBaseInfoService.createCourseBase(companyId, addCourseDto);
        return courseBase;
    }

    /**
     * 根据课程id查询课程信息
     *
     * @param courseId
     * @return
     */
    @GetMapping("/course/{courseId}")
    @ApiOperation("根据id查询课程接口")
    public CourseBaseInfoDto getCourseBaseById(@PathVariable("courseId") Long courseId) {
        if (courseId == null) {
            log.debug("课程id不能为空:{}", courseId);
            XueChengPlusException.cast("课程id不能为空");
        }
        CourseBaseInfoDto courseBaseInfoDto = courseBaseInfoService.getCourseBaseById(courseId);
        return courseBaseInfoDto;
    }

    @PutMapping("/course")
    @ApiOperation("修改课程接口")
    public CourseBaseInfoDto modifyCourseBase(@RequestBody
                                              @Validated(value = {ValidationGroups.Update.class})
                                              EditCourseDto editCourseDto) {
        if (editCourseDto == null) {
            XueChengPlusException.cast("传入的课程信息为空，不能查询到课程信息");
        }
        Long companyId = 1L;
        CourseBaseInfoDto courseBaseInfoDto = courseBaseInfoService.modifyCourseBase(companyId, editCourseDto);
        return courseBaseInfoDto;
    }


}
